package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.RatingBar;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.view.item.DetailsFruitsAndVegetablesView;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.BannersAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleaningActivity.CLEANING_BEAN;

/**
 * Created by Administrator on 2017/8/23.
 * 购物车
 */

public class DetailsFruitsAndVegetablesActivity extends Activity implements View.OnClickListener {
    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.title_center)
    TextView title_center;
    @Bind(R.id.bottom_options)
    LinearLayout bottom_options;
    @Bind(R.id.shopCartNum)
    TextView shopCartNum;
    @Bind(R.id.contact)
    TextView contact;
    @Bind(R.id.all_goods)
    ImageView allGoods;
    @Bind(R.id.goods_layout)
    RelativeLayout goodsLayout;
    @Bind(R.id.appraise_img)
    ImageView appraiseImg;
    @Bind(R.id.appraise_layout)
    RelativeLayout appraiseLayout;
    @Bind(R.id.contentLayout)
    LinearLayout contentLayout;
    @Bind(R.id.ad_pager)
    ViewPager adPager;
    @Bind(R.id.ad_layer)
    LinearLayout adLayer;
    @Bind(R.id.merchant_name)
    TextView mMerchantName;
    @Bind(R.id.merchant_interduce)
    TextView mMerchantInderduce;
    @Bind(R.id.address)
    TextView mMerchantAddress;
    @Bind(R.id.rating)
    RatingBar mRat;
    @Bind(R.id.order_count)
    TextView mOrderCount;
    @Bind(R.id.total_price)
    TextView mTotalPrice;
    @Bind(R.id.imageView3)
    ImageView mBugCar;
    private CallOrderBean mBean;
    private List<CleanProductBean> mProductBean = new ArrayList<>();
    private final int HOME_AD_RESULT = 1;
    private double mPrice;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                // 广告
                case HOME_AD_RESULT:
                    adPager.setCurrentItem(adPager.getCurrentItem() + 1,
                            true);
                    break;
            }
        }


    };
    Context context;
    String pos = "0";//0、干洗  1、蔬果
    DetailsFruitsAndVegetablesView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details_fruits_and_vegetables);
        ButterKnife.bind(this);
        context = this;
        initView();
    }

    public void getProductMessage(List<CleanProductBean> bean) {
        mPrice = 0;
        mProductBean.clear();
        mProductBean.addAll(bean);
        for (int i= 0; i < mProductBean.size(); i++) {
            mPrice +=  Double.valueOf(mProductBean.get(i).price);
        }
        if (mProductBean != null && mProductBean.size() > 0) {
            mTotalPrice.setText("￥"+String.valueOf(mPrice));
            mTotalPrice.setTextColor(getResources().getColor(R.color.statefocused));
            contact.setText("去结算");
            contact.setTextColor(getResources().getColor(R.color.white));
            contact.setBackgroundResource(R.color.statefocused);
        } else {
            mTotalPrice.setText("￥0.00");
            mTotalPrice.setTextColor(getResources().getColor(R.color.bottom_text));
            contact.setText("未选购");
            contact.setTextColor(getResources().getColor(R.color.white));
            contact.setBackgroundResource(R.color.bottom_text);
        }
    }

    private void initView() {
        retur.setOnClickListener(this);
        contact.setOnClickListener(this);
        goodsLayout.setOnClickListener(this);
        appraiseLayout.setOnClickListener(this);
        mBugCar.setOnClickListener(this);
        if (getIntent() != null) {
            pos = getIntent().getStringExtra("pos");
            mBean = (CallOrderBean) getIntent().getSerializableExtra(CLEANING_BEAN);
        }
        if (pos.equals("0")) {
            title_center.setText("店铺详情");
        } else {
            title_center.setText("蔬果详情");
        }
        LoadData();
        view = new DetailsFruitsAndVegetablesView(context, pos);
        contentLayout.addView(view);
        processVehicleDetails();
    }

    private void LoadData() {
        mMerchantName.setText(mBean.name);
        mMerchantInderduce.setText(mBean.profile);
        mMerchantAddress.setText("地址:" + mBean.suite);
        mRat.setStar((float) mBean.star);
        mOrderCount.setText("订单总数:" + String.valueOf(mBean.order));
        if (mBean.figurepic.length() > 0) {
            List<String> pic = new ArrayList<>();
            String[] split = mBean.figurepic.split("\\|");
            for (int i = 0; i < split.length; i++) {
                String s = split[i];
                pic.add(i, s);
            }
            getadvertising(pic.size(), pic);
        }
    }

    //添加商品
    private void processVehicleDetails() {
        view.setData(0, this, shopCartNum);
    }

    //添加评论
    private void processEvaluation() {
        view.setData(1, this, shopCartNum);
    }

    //轮播广告
    public void getadvertising(final int size, final List<String> getinforresult) {
        adPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                Sundry.refreshPoint(adLayer, position % size, 0);
                if (mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
                mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                if (ViewPager.SCROLL_STATE_DRAGGING == arg0
                        && mHandler.hasMessages(HOME_AD_RESULT)) {
                    mHandler.removeMessages(HOME_AD_RESULT);
                }
            }
        });
        BannersAdapter adapter = new BannersAdapter(context, getinforresult, 0);
        adPager.setAdapter(adapter);
        Sundry.initPointsLayout(adLayer, context, size, 0);
        adPager.setCurrentItem(size * 1000, false);
        // 自动轮播线程
        mHandler.sendEmptyMessageDelayed(HOME_AD_RESULT, 3 * 1000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.contact:
                if (CheckData()) {
                    Intent intent = new Intent(context, ShopOrderActivity.class);
                    intent.putExtra("pos", pos);
                    startActivity(intent);
                }
                break;
            case R.id.goods_layout:
                processVehicleDetails();
                allGoods.setVisibility(View.VISIBLE);
                appraiseImg.setVisibility(View.GONE);
                bottom_options.setVisibility(View.VISIBLE);
                break;
            case R.id.appraise_layout:
                processEvaluation();
                allGoods.setVisibility(View.GONE);
                appraiseImg.setVisibility(View.VISIBLE);
                bottom_options.setVisibility(View.GONE);
                break;
            case R.id.imageView3:
                if (mProductBean != null && mProductBean.size() > 0) {
                    showPup();
                }
                break;
        }
    }

    /**
     * 弹出 购物车的popuwindow
     *
     * @return
     */
    private void showPup() {

    }

    private boolean CheckData() {

        return true;
    }
}
